package org.seedltd.upms.controller.sys;

import org.seedltd.core.base.BaseController;
import org.seedltd.core.base.BasePage;
import org.seedltd.core.utils.Assert;
import org.seedltd.core.utils.BeanUtils;
import org.seedltd.core.validation.Save;
import org.seedltd.core.validation.Update;
import org.seedltd.log.annotation.SysLog;
import org.seedltd.upms.model.sys.entity.SysParam;
import org.seedltd.upms.model.sys.form.SysParamForm;
import org.seedltd.upms.model.sys.form.SysParamSearchForm;
import org.seedltd.upms.model.sys.vo.SysParamVO;
import org.seedltd.upms.service.sys.SysParamService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import lombok.AllArgsConstructor;

/**
 *
 * 参数管理控制器
 * 
 * @author laizuan
 * @version 1.0
 * @since 2021/07/19
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sysParam")
public class SysParamController extends BaseController<SysParamService> {

    /**
     * 新增
     * 
     * @param form
     *            参数管理表单数据
     * @return 参数管理主键
     */
    @SysLog("新增参数数据")
    @PostMapping(value = "/add")
    @PreAuthorize("hasAuthority('sys:param:add')")
    public Long add(@RequestBody @Validated(Save.class) SysParamForm form) {
        return baseService.add(form).getParamId();
    }

    /**
     * 获取详情
     * 
     * @param paramKey
     *            参数key
     * @return 参数管理
     */
    @GetMapping(value = "/get")
    public SysParamVO get(String paramKey) {
        Assert.notNull(paramKey, "参数不能为空");
        SysParam pojo = baseService.getByKey(paramKey);
        Assert.notNull(pojo, "未找到数据");
        SysParamVO pojoVo = new SysParamVO();
        BeanUtils.copyProperties(pojo, pojoVo);
        return pojoVo;
    }

    /**
     * 修改
     * 
     * @param form
     *            参数管理表单数据
     * @return 修改状态。true标识修改成功，false标识修改失败
     */
    @SysLog("修改参数数据")
    @PostMapping(value = "/update")
    @PreAuthorize("hasAuthority('sys:param:update')")
    public boolean update(@RequestBody @Validated({Save.class, Update.class}) SysParamForm form) {
        return baseService.update(form);
    }

    /**
     * 删除
     * 
     * @param paramKey
     *            参数Key
     * @return 删除状态。true标识修改成功，false标识修改失败
     */
    @SysLog("删除参数数据")
    @GetMapping(value = "/delete")
    @PreAuthorize("hasAuthority('sys:param:delete')")
    public boolean delete(String paramKey) {
        Assert.notNull(paramKey, "paramKey不能为空");
        return baseService.deleteByKey(paramKey);
    }

    /**
     * 列表
     * 
     * @param searchForm
     *            查询条件
     * @return 参数管理列表集合
     */
    @PostMapping(value = "/list")
    public BasePage<SysParamVO> page(@RequestBody SysParamSearchForm searchForm) {
        return baseService.page(searchForm);
    }

}